package com.medical.wechat.web.controller;

import com.medical.common.JsonResult;
import com.medical.wechat.model.WechatAccount;
import com.medical.wechat.model.WechatMenu;
import com.medical.wechat.service.WechatAccountService;
import com.medical.wechat.service.WechatMenuService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * Created by Administrator on 2016/10/21.
 */
@SuppressWarnings("SpringJavaAutowiringInspection")
@Controller
@RequestMapping("/wechatMenu")
public class WechatMenuController {
    @Autowired
    private WechatMenuService wechatMenuService;
    @Autowired
    private WechatAccountService wechatAccountService;

    //根据accountId查询菜单栏
    @ApiOperation(value="根据accountId查询菜单栏", notes="根据accountId查询菜单栏")
    @RequestMapping(value="/selectMenu/{accountId}",method = RequestMethod.GET)
    @ResponseBody
    public JsonResult selectMenuByAccountId(@PathVariable String accountId){
        return new JsonResult("200","",wechatMenuService.selectMenuByAccountId(accountId));
    }


    //根据accountId新增菜单栏
    @ApiOperation(value="根据accountId新增菜单栏", notes="根据accountId新增菜单栏")
    @RequestMapping(value = "/addMenu/{accountId}",method = RequestMethod.POST)
    @ResponseBody
    public JsonResult patchMenuByAccountId(@PathVariable String accountId,
                                           @RequestParam(value = "parentId" ,required = false)String parentId,
                                           @RequestParam(value = "name" ,required = false)String name,
                                           @RequestParam(value = "type" ,required = false)String type,
                                           @RequestParam(value = "key" ,required = false)String key,
                                           @RequestParam(value = "sort" ,required = false)Integer sort
                                           ){
        WechatMenu wechatMenu=new WechatMenu();
        wechatMenu.setId(UUID.randomUUID().toString());
        wechatMenu.setParentId(parentId);
        wechatMenu.setName(name);
        wechatMenu.setType(type);
        wechatMenu.setKey(key);
        wechatMenu.setSort(sort);
        wechatMenuService.createWechatMenu(wechatMenu);

        return new JsonResult();
    }

    //根据accountId返回一级菜单List
    @RequestMapping(value = "/getparent/{accountId}",method = RequestMethod.GET)
    @ResponseBody
    public JsonResult selectByParentId(@PathVariable String accountId){
        try{
            List<WechatMenu> wechatMenus=wechatMenuService.selectByParentId("0");
            List<WechatMenu> wechatMenuList = new ArrayList<>();
            for(WechatMenu wechatMenu:wechatMenus){
                if (wechatMenu.getAccountId().equals(accountId)){
                    wechatMenuList.add(wechatMenu);
                }
            }
        System.out.println(wechatMenuList.toString());
            return new JsonResult("200","",wechatMenuList);
        }catch (Exception e){
            return new JsonResult("500",e.getMessage());
        }
    }

    //编辑
    @ApiOperation(value="编辑", notes="编辑")
    @RequestMapping(value = "/patch/{id}",method = RequestMethod.POST)
    @ResponseBody
    public JsonResult patchMenuById(@PathVariable String id,
                                    @RequestParam(value = "parentId" ,required = false)String parentId,
                                    @RequestParam(value = "name" ,required = false)String name,
                                    @RequestParam(value = "type" ,required = false)String type,
                                    @RequestParam(value = "key" ,required = false)String key,
                                    @RequestParam(value = "sort" ,required = false)Integer sort){
        WechatMenu wechatMenu=wechatMenuService.findById(id);
        wechatMenu.setParentId(parentId);
        wechatMenu.setName(name);
        wechatMenu.setType(type);
        wechatMenu.setKey(key);
        wechatMenu.setSort(sort);
        wechatMenuService.updateById(wechatMenu);
        return new JsonResult();
    }
    //删除
    @ApiOperation(value="删除", notes="删除")
    @RequestMapping(value="/delete/{id}",method = RequestMethod.POST)
    @ResponseBody
    public  JsonResult deleteMenu(@PathVariable String id){
        wechatMenuService.deleteWechatMenuById(id);
        return new JsonResult();
    }

    //生成微信公众号菜单
    @ApiOperation(value="生成微信公众号菜单", notes="生成微信公众号菜单")
    @RequestMapping(value="/generateMenu/{accountId}",method = RequestMethod.GET)
    @ResponseBody
    public JsonResult generateMenu(@PathVariable String accountId){

        String result = wechatMenuService.generateWechatMenu(accountId);

        return new JsonResult("200","",result);
    }


    //生成默认微信公众号菜单
    @ApiOperation(value="生成默认微信公众号菜单", notes="生成默认微信公众号菜单")
    @RequestMapping(value="/generateDefaultMenu/{accountId}/{userId}",method = RequestMethod.GET)
    @ResponseBody
    public JsonResult generateDefaultMenu(@PathVariable String accountId ,@PathVariable String userId){

        String result = wechatMenuService.generateDefaultMenu(accountId, userId);

        return new JsonResult("200","",result);
    }
    //把url地址转换成微信网页授权回调域名
    @ApiOperation(value="把url地址转换成微信网页授权回调域名", notes="把url地址转换成微信网页授权回调域名")
    @RequestMapping(value="/changeMenuToOAuth/{accountId}/{root}/{forword}",method = RequestMethod.GET)
    @ResponseBody
    public JsonResult changeMenuTo(@PathVariable String accountId,@PathVariable String root,
                               @PathVariable String forword){

        String url = wechatMenuService.changeMenuToOAuth(accountId, root, forword);

        return new JsonResult("200","",url);
    }
}
